package com.example.testapplication.fragment;

import android.util.Log;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.testapplication.R;
import com.example.testapplication.adapter.AddressAdapter;
import com.example.testapplication.api.Api;
import com.example.testapplication.api.ApiConfig;
import com.example.testapplication.api.CallBack;
import com.example.testapplication.entity.AddressEntity;
import com.example.testapplication.entity.response.AddressResponse;
import com.google.gson.Gson;
import com.scwang.smart.refresh.footer.ClassicsFooter;
import com.scwang.smart.refresh.header.ClassicsHeader;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnLoadMoreListener;
import com.scwang.smart.refresh.layout.listener.OnRefreshListener;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class AddressFragment extends BaseFragment {
    private RecyclerView recyclerView;
    private RefreshLayout refreshLayout;
    private AddressAdapter addressAdapter;
    List<AddressEntity> datas = new ArrayList<>();

    public static AddressFragment newInstance() {
        AddressFragment fragment = new AddressFragment();
        return fragment;
    }

    @Override
    protected int initLayout() {
        return R.layout.fragment_address;
    }

    @Override
    protected void initView() {
        recyclerView = mRootView.findViewById(R.id.recyclerViewAddress);
        refreshLayout = mRootView.findViewById(R.id.refreshLayoutAddress);
        refreshLayout.setRefreshHeader(new ClassicsHeader(getActivity()));
        refreshLayout.setRefreshFooter(new ClassicsFooter(getActivity()));

    }

    @Override
    protected void initData() {

        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity());
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(linearLayoutManager);
        addressAdapter=new AddressAdapter(getActivity());
        recyclerView.setAdapter(addressAdapter);

        refreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                getAddressList(true);
            }
        });
        refreshLayout.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(RefreshLayout refreshlayout) {
                //refreshlayout.finishLoadMore(2000/*,false*/);//传入false表示加载失败
                getAddressList(false);
            }
        });
        getAddressList(true);

    }


    private void getAddressList(boolean isRefresh) {
        HashMap<String,Object> params = new HashMap<>();
        Api.config(ApiConfig.USER_ADDRESSLIST,params).getRequest(getActivity(), new CallBack() {
            @Override
            public void onSuccess(String result) {
                getActivity().runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        if(isRefresh){
                            refreshLayout.finishRefresh(true);showToast("加载完成");
                        }else{
                            refreshLayout.finishLoadMore(true);
                        }
                        Log.v("---vnvx",result);
                        Gson gson = new Gson();
                        AddressResponse res = gson.fromJson(result, AddressResponse.class);
                        if(res.getCode() == 1){
                            Log.v("nima-->",result);
                            List<AddressEntity> lists = res.getData();
                            if(lists!=null){
                                datas = lists;
                                addressAdapter.setListDatas(datas);
                                addressAdapter.notifyDataSetChanged();
                            }else {
                                if(isRefresh){
                                    showToast("暂时无法加载");
                                }else{
                                    showToast("没有更多啦");
                                }
                            }
                        }else{
                            showToastAsync("失败");
                        }
                    }
                });

            }

            @Override
            public void onFailure(IOException e) {
                Log.v("---vnvx","result");
            }
        });
    }

}
